package com.codepilot.server.service.impl;

import com.codepilot.server.mapper.StatsMapper;
import com.codepilot.server.model.dto.UserDTO;
import com.codepilot.server.model.entity.User;
import com.codepilot.server.model.exception.enums.GlobalErrorCodeConstants;
import com.codepilot.server.model.vo.CommonResult;
import com.codepilot.server.model.vo.DifficultyDistrbVO;
import com.codepilot.server.model.vo.StatsVO;
import com.codepilot.server.model.vo.TopicDistrbVO;
import com.codepilot.server.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class StatsServiceImpl implements StatsService {

    @Autowired
    private StatsMapper statsMapper;

    @Override
    public CommonResult<StatsVO> getStats() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDTO loginUser = (UserDTO) authentication.getPrincipal();
        User user = loginUser.getUser();
        Random random = new Random();

        try {
//            Statistics statistics = statsMapper.getStatsByUserId(loginUser.getUser().getId());
//            StatsDTO statsDTO = StatsConverter.INSTANCE.statistics2StatsDTO(statistics);
//            StatsVO statsVO = StatsConverter.INSTANCE.statsDTO2StatsVO(statsDTO);
            StatsVO statsVO = StatsVO.builder().userId(user.getId()).build();
            statsVO.setTotal(statsMapper.getTotalDoneRecords(user.getId()));
            statsVO.setCorrectRate((float) random.nextInt(statsVO.getTotal() + 1) / statsVO.getTotal());
            long startTime = statsMapper.getStartTime(user.getId()).getTime();
            long endTime = statsMapper.getEndTime(user.getId()).getTime();
            long days = (endTime - startTime) / (1000L * 3600 * 24);
            statsVO.setDays((int) days);

            DifficultyDistrbVO difficultyDistrbVO = DifficultyDistrbVO.builder()
                    .easy(statsMapper.getDifficultyCount(user.getId(), "%Easy%"))
                    .medium(statsMapper.getDifficultyCount(user.getId(), "%Medium"))
                    .hard(statsMapper.getDifficultyCount(user.getId(), "Hard"))
                    .build();
            TopicDistrbVO topicDistrbVO = TopicDistrbVO.builder()
                    .haxi(statsMapper.getTopicCount(user.getId(), "%Hash%"))
                    .shu(statsMapper.getTopicCount(user.getId(), "%Tree%"))
                    .tu(statsMapper.getTopicCount(user.getId(), "%Graph%"))
                    .shuzu(statsMapper.getTopicCount(user.getId(), "%Array%"))
                    .zifuchuan(statsMapper.getTopicCount(user.getId(), "%String%"))
                    .dongtaiguihua(statsMapper.getTopicCount(user.getId(), "%Dynamic%"))
                    .shuxue(statsMapper.getTopicCount(user.getId(), "%Math%"))
                    .paixu(statsMapper.getTopicCount(user.getId(), "%Sorting%"))
                    .tanxin(statsMapper.getTopicCount(user.getId(), "%Greedy%")).build();

            statsVO.setDifficultyDistrb(difficultyDistrbVO);
            statsVO.setTopicDistrb(topicDistrbVO);
            return CommonResult.success(statsVO);
        } catch (Exception e) {
            return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
        }
    }

}
